<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="90px">
      <el-form-item label="公司ID" prop="companyId">
        <el-input
          v-model="queryParams.companyId"
          placeholder="请输入公司ID"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="AppId" prop="appId">
        <el-input
          v-model="queryParams.appId"
          placeholder="请输入AppId"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="CorpId" prop="corpid">
        <el-input
          v-model="queryParams.corpid"
          placeholder="请输入CorpId"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="SuiteId" prop="suiteId">
        <el-input
          v-model="queryParams.suiteId"
          placeholder="请输入SuiteId"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="AgentId" prop="agentid">
        <el-input
          v-model="queryParams.agentid"
          placeholder="请输入AgentId"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="应用名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入应用名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="Logo" prop="squareLogoUrl">
        <el-input
          v-model="queryParams.squareLogoUrl"
          placeholder="请输入Logo"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="权限等级" prop="level">
        <el-input
          v-model="queryParams.level"
          placeholder="请输入权限等级"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="永久授权码" prop="permanentCode">
        <el-input
          v-model="queryParams.permanentCode"
          placeholder="请输入永久授权码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="安装人员" prop="userid">
        <el-input
          v-model="queryParams.userid"
          placeholder="请输入安装人员"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="安装人名" prop="username">
        <el-input
          v-model="queryParams.username"
          placeholder="请输入安装人名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="应用等级" prop="vipLevel">
        <el-input
          v-model="queryParams.vipLevel"
          placeholder="请输入应用等级"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="过期时间" prop="overtime">
        <el-date-picker clearable
          v-model="queryParams.overtime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择过期时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="购买人数" prop="people">
        <el-input
          v-model="queryParams.people"
          placeholder="请输入购买人数"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="创建时间" prop="createdAt">
        <el-date-picker clearable
          v-model="queryParams.createdAt"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择创建时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="更新时间" prop="updatedAt">
        <el-date-picker clearable
          v-model="queryParams.updatedAt"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择更新时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="3" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:corp_company_agents:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:corp_company_agents:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:corp_company_agents:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:corp_company_agents:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="corp_company_agentsList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="45" align="center" />
      <el-table-column label="ID" align="center" width="50" prop="id" />
      <el-table-column label="公司ID" align="center" prop="companyId" />
      <el-table-column label="AppId" align="center" prop="appId" />
      <el-table-column label="CorpId" align="center" width="170" prop="corpid" />
      <el-table-column label="SuiteId" align="center" width="170" prop="suiteId" />
      <el-table-column label="AgentId" align="center" prop="agentid" />
      <el-table-column label="应用名称" align="center" prop="name" />
      <el-table-column label="Logo" align="center" width="300" :show-overflow-tooltip="true" prop="squareLogoUrl" />
      <el-table-column label="权限等级" align="center" prop="level" />
      <el-table-column label="永久授权码" align="center" width="170" :show-overflow-tooltip="true" prop="permanentCode" />
      <el-table-column label="可见部门" align="center" width="170" :show-overflow-tooltip="true" prop="allowParty" />
      <el-table-column label="可见用户" align="center" width="170" :show-overflow-tooltip="true" prop="allowUser" />
      <el-table-column label="可见标签" align="center" width="170" :show-overflow-tooltip="true" prop="allowTag" />
      <el-table-column label="额外部门" align="center" prop="extraParty" />
      <el-table-column label="额外成员" align="center" prop="extraUser" />
      <el-table-column label="额外标签" align="center" prop="extraTag" />
      <el-table-column label="安装人员" align="center" prop="userid" />
      <el-table-column label="安装人名" align="center" prop="username" />
      <el-table-column label="头像" align="center" width="170" :show-overflow-tooltip="true" prop="avatar" />
      <el-table-column label="应用等级" align="center" prop="vipLevel" />
      <el-table-column label="过期时间" align="center" prop="overtime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.overtime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="购买人数" align="center" prop="people" />
      <el-table-column label="应用管理员" align="center" width="170" :show-overflow-tooltip="true" prop="admin" />
      <el-table-column label="创建时间" align="center" prop="createdAt" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createdAt, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="更新时间" align="center" prop="updatedAt" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.updatedAt, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" width="110px" fixed="right" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:corp_company_agents:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:corp_company_agents:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改授权应用对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="公司ID" prop="companyId">
          <el-input v-model="form.companyId" placeholder="请输入公司ID" />
        </el-form-item>
        <el-form-item label="AppId" prop="appId">
          <el-input v-model="form.appId" placeholder="请输入AppId" />
        </el-form-item>
        <el-form-item label="CorpId" prop="corpid">
          <el-input v-model="form.corpid" placeholder="请输入CorpId" />
        </el-form-item>
        <el-form-item label="SuiteId" prop="suiteId">
          <el-input v-model="form.suiteId" placeholder="请输入SuiteId" />
        </el-form-item>
        <el-form-item label="AgentId" prop="agentid">
          <el-input v-model="form.agentid" placeholder="请输入AgentId" />
        </el-form-item>
        <el-form-item label="应用名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入应用名称" />
        </el-form-item>
        <el-form-item label="Logo" prop="squareLogoUrl">
          <el-input v-model="form.squareLogoUrl" placeholder="请输入Logo" />
        </el-form-item>
        <el-form-item label="权限等级" prop="level">
          <el-input v-model="form.level" placeholder="请输入权限等级" />
        </el-form-item>
        <el-form-item label="永久授权码" prop="permanentCode">
          <el-input v-model="form.permanentCode" placeholder="请输入永久授权码" />
        </el-form-item>
        <el-form-item label="可见部门" prop="allowParty">
          <el-input v-model="form.allowParty" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="可见用户" prop="allowUser">
          <el-input v-model="form.allowUser" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="可见标签" prop="allowTag">
          <el-input v-model="form.allowTag" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="额外部门" prop="extraParty">
          <el-input v-model="form.extraParty" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="额外成员" prop="extraUser">
          <el-input v-model="form.extraUser" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="额外标签" prop="extraTag">
          <el-input v-model="form.extraTag" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="安装用户ID" prop="userid">
          <el-input v-model="form.userid" placeholder="请输入安装用户ID" />
        </el-form-item>
        <el-form-item label="安装人姓名" prop="username">
          <el-input v-model="form.username" placeholder="请输入安装人姓名" />
        </el-form-item>
        <el-form-item label="安装人头像" prop="avatar">
          <el-input v-model="form.avatar" placeholder="请输入安装人头像" />
        </el-form-item>
        <el-form-item label="应用等级" prop="vipLevel">
          <el-input v-model="form.vipLevel" placeholder="请输入应用等级" />
        </el-form-item>
        <el-form-item label="过期时间" prop="overtime">
          <el-date-picker clearable
            v-model="form.overtime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择过期时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="购买人数" prop="people">
          <el-input v-model="form.people" placeholder="请输入购买人数" />
        </el-form-item>
        <el-form-item label="应用管理员" prop="admin">
          <el-input v-model="form.admin" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="创建时间" prop="createdAt">
          <el-date-picker clearable
            v-model="form.createdAt"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择创建时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="更新时间" prop="updatedAt">
          <el-date-picker clearable
            v-model="form.updatedAt"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择更新时间">
          </el-date-picker>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listCorp_company_agents, getCorp_company_agents, delCorp_company_agents, addCorp_company_agents, updateCorp_company_agents } from "@/api/admin/corp_company_agents";

export default {
  name: "Corp_company_agents",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: false,
      // 总条数
      total: 0,
      // 授权应用表格数据
      corp_company_agentsList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        companyId: null,
        appId: null,
        corpid: null,
        suiteId: null,
        agentid: null,
        name: null,
        squareLogoUrl: null,
        level: null,
        permanentCode: null,
        userid: null,
        username: null,
        vipLevel: null,
        overtime: null,
        people: null,
        createdAt: null,
        updatedAt: null
      },
      // 表单参数
      form: {},
      // 显示的列
      columns: [
        { key: 0, label: `Corpid`, visible: true },
        { key: 1, label: `名称`, visible: true },
        { key: 2, label: `类型`, visible: true },
        { key: 3, label: `用户规模`, visible: true },
        { key: 4, label: `AgentMax`, visible: true },
        { key: 5, label: `规模`, visible: true },
        { key: 6, label: `类型`, visible: true },
        { key: 7, label: `所属行业`, visible: true },
        { key: 8, label: `所属子行业`, visible: true },
        { key: 9, label: `所在地信息`, visible: true },
        { key: 10, label: `会员等级`, visible: true },
        { key: 11, label: `状态`, visible: true },
        { key: 12, label: `Logo`, visible: true },
        { key: 13, label: `二维码`, visible: true },
        { key: 14, label: `新增时间`, visible: true },
        { key: 15, label: `更新时间`, visible: true }
      ],
      // 表单校验
      rules: {
        companyId: [
          { required: true, message: "公司ID不能为空", trigger: "blur" }
        ],
        appId: [
          { required: true, message: "AppId不能为空", trigger: "blur" }
        ],
        corpid: [
          { required: true, message: "CorpId不能为空", trigger: "blur" }
        ],
        suiteId: [
          { required: true, message: "SuiteId不能为空", trigger: "blur" }
        ],
        agentid: [
          { required: true, message: "AgentId不能为空", trigger: "blur" }
        ],
        name: [
          { required: true, message: "应用名称不能为空", trigger: "blur" }
        ],
        squareLogoUrl: [
          { required: true, message: "Logo不能为空", trigger: "blur" }
        ],
        level: [
          { required: true, message: "权限等级不能为空", trigger: "blur" }
        ],
        permanentCode: [
          { required: true, message: "永久授权码不能为空", trigger: "blur" }
        ],
        userid: [
          { required: true, message: "安装用户ID不能为空", trigger: "blur" }
        ],
        username: [
          { required: true, message: "安装人姓名不能为空", trigger: "blur" }
        ],
        avatar: [
          { required: true, message: "安装人头像不能为空", trigger: "blur" }
        ],
        vipLevel: [
          { required: true, message: "应用等级不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询授权应用列表 */
    getList() {
      this.loading = true;
      listCorp_company_agents(this.queryParams).then(response => {
        this.corp_company_agentsList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        companyId: null,
        appId: null,
        corpid: null,
        suiteId: null,
        agentid: null,
        name: null,
        squareLogoUrl: null,
        level: null,
        permanentCode: null,
        allowParty: null,
        allowUser: null,
        allowTag: null,
        extraParty: null,
        extraUser: null,
        extraTag: null,
        userid: null,
        username: null,
        avatar: null,
        vipLevel: null,
        overtime: null,
        people: null,
        admin: null,
        createdAt: null,
        updatedAt: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加授权应用";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getCorp_company_agents(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改授权应用";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateCorp_company_agents(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addCorp_company_agents(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除授权应用编号为"' + ids + '"的数据项？').then(function() {
        return delCorp_company_agents(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/corp_company_agents/export', {
        ...this.queryParams
      }, `corp_company_agents_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>

<style scoped>
.el-form-item--small.el-form-item{
  margin-bottom: 10px;
}
/deep/ label{
  font-weight: 500;
}
/deep/ .el-date-editor.el-input, .el-date-editor.el-input__inner {
  width: 215px;
}
</style>
